const Router = require('koa-router');
const axios = require('./axiosDefault');

const router = new Router();

router
  .get('/api', async (ctx, next) => {
    console.log('get');
    ctx.body = await axios
      .get('/planetary/apod', {
        params: ctx.query,
        timeout: 10000
      })
      .then(resp => {
        return resp.data;
      })
      .catch(error => console.log(`error:  ${error}`));
  })
  .post('/p-api', async (ctx, next) => {
    console.log('post');
    ctx.body = await axios
      .get('/planetary/apod?api_key=DEMO_KEY', {
        params: ctx.request.body
      })
      .then(resp => {
        return resp.data;
        // return ctx.request.body;
      })
      .catch(error => console.log(`error:  ${error}`));
  })
  .get('/api/hello', (ctx, next) => {
    console.log('get');
    ctx.body = 'hello';
  })
  .post('/hello', (ctx, next) => {
    console.log(ctx.url);
    ctx.body = {
      name: 'wsz'
    };
  })
  .post('/*', (ctx, next) => {
    ctx.body = '404!!!';
  });

module.exports = router;
